<template>
	<view class="">
		<view class="search-box">
			<uni-search-bar @input="input" :radius="100" cancelButton="none"></uni-search-bar>
		</view>
		<!-- 删除搜索记录 -->
		<view class="search-delete" v-if="this.searchLearn[0] && this.kw === ''">
			<text>搜索历史</text>
			<uni-icons type="trash" @click="clearAll"></uni-icons>
		</view>
		<!-- 搜索结果列表 -->
		<view class="search-item" v-for="(item, index) in searchResults" :key="index" @click="gotoDetail(item.goods_id)">
			<text class="text-search">{{ item.goods_name }}</text>
			<uni-icons type="forward"></uni-icons>
		</view>
		<!-- 搜索记录 -->
		<view class="search-save" v-for="(item, index) in searchLearn" :key="index" v-if="kw===''">
			<view class="text-save" @click="gotoGoodsList(item)">{{ item }}</view>
			<uni-icons type="closeempty" @click="clearOne(item)" size="14"></uni-icons>
		</view>
	</view>
	
</template>

<script>
	export default {
		data() {
			return {
				// 延时器 timeId
				timer: null,
				kw: '' ,//搜索的关键字
				searchResults: [], //搜索结果
				searchLearn: [], //搜索记录
			};
		},
		onLoad() {
			this.searchLearn = JSON.parse(uni.getStorageSync('kw') || '[]')
		},
		methods: {
			input(e){
				// 清除延时器
				clearTimeout(this.timer)
				// 重新启动一个延时器，并把timerId赋值给 this.timer
				this.timer = setTimeout(() => {
					this.kw = e
					// console.log(this.kw);
					this.getSearchList()
					if(e!==''){
						this.saveSearchList()
					}
				}, 500)
			},
			async getSearchList(){
				//判断关键词是否为空
				if(this.kw === ''){
					this.searchResults = []
					return
				}
				const {data: res} = await uni.$http.get('/api/public/v1/goods/qsearch', {query: this.kw})
				//请求失败
				if(res.meta.status !== 200) return uni.$showMsg()
				this.searchResults = res.message
			},
			saveSearchList(){
				const set=new Set(this.searchLearn)
				set.add(this.kw)
				this.searchLearn=Array.from(set)
				if(this.searchLearn[0]!==this.kw){
					this.searchLearn.reverse()
				}
				uni.setStorageSync('kw', JSON.stringify(this.searchLearn))
			},
			clearAll(){
				const that = this
				uni.showModal({
					title: '您确定要全部删除吗？',
					// content: '这是一个模态弹窗',
					success: function (res) {
						if (res.confirm) {
							that.searchLearn = []
							uni.setStorageSync('kw', '[]')
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			clearOne(item){
				const that = this
				uni.showModal({
					title: '您确定要删除这一项吗？',
					// content: '这是一个模态弹窗',
					success: function (res) {
						if (res.confirm) {
							let temp = new Set(that.searchLearn)
							temp.delete(item)
							that.searchLearn = Array.from(temp)
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			gotoDetail(goods_id){
				uni.navigateTo({
					url:`/subpkg/goods_detail/goods_detail?goods_id=${goods_id}`
				})
			},
			gotoGoodsList(item){
				uni.navigateTo({
					url:`/subpkg/goods_list/goods_list?query=${item}`
				})
			}
		}
	}
</script>

<style lang="scss">
.search-box{
	position: sticky;
	top: 0;
	z-index: 999;
}
.search-delete{
	display: flex;
	justify-content: space-between;
	padding: 3px 8px;
	border-bottom: 1px solid #ccc;
}
.search-item{
	padding: 10px;
	border-bottom: 1px solid #ccc;
	.text-search{
		width: 95%;
		text-overflow: ellipsis;
		overflow: hidden;
		white-space: nowrap;
		display: inline-block;
		vertical-align: top;
	}
}
.search-item:last-child{
	border-bottom: none;
}
.search-save{
	display: inline-block;
	margin: 3px;
	background-color: #9e9e9e;
	border-radius: 5px;
	padding: 3px;
	.text-save{
		max-width: 80px;
		text-overflow: ellipsis;
		overflow: hidden;
		white-space: nowrap;
		display: inline-block;
		vertical-align: top;
	}
}
</style>
